package com.hertz.iris.view.alarm;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import antistatic.spinnerwheel.AbstractWheel;
import antistatic.spinnerwheel.OnWheelClickedListener;
import antistatic.spinnerwheel.WheelVerticalView;
import antistatic.spinnerwheel.adapters.NumericWheelAdapter;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

import com.actionbarsherlock.app.SherlockFragment;
import com.hertz.iris.IrisApplication;
import com.hertz.iris.R;
import com.hertz.iris.pojo.NormalAlarm;
import com.hertz.iris.util.BundleKeys;

public class HHMMSelectorFragment extends SherlockFragment {
	
	@InjectView(R.id.hh_lv)
	WheelVerticalView hhLv;
	@InjectView(R.id.mm_lv)
	WheelVerticalView mmLv;
	@InjectView(R.id.cancel_btn)
	Button cancelBtn;
	@InjectView(R.id.ok_btn)
	Button okBtn;
	
	OnSetHM myCallback;
	
	public interface OnSetHM{
		public void setHM(byte hh,byte mm);
	}
	
	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
		myCallback = (OnSetHM) activity;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		super.onCreateView(inflater, container, savedInstanceState);
		View hhmmSelectorDialog = inflater.inflate(R.layout.hhmm_selector_frag, container, false);
		ButterKnife.inject(this,hhmmSelectorDialog);
		
		okBtn.setTypeface(IrisApplication.getInstance().getTypeface());
		cancelBtn.setTypeface(IrisApplication.getInstance().getTypeface());
		return hhmmSelectorDialog;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		NumericWheelAdapter hhAdapter = new NumericWheelAdapter(getActivity(), 0, 23);
		hhAdapter.setItemResource(R.layout.simple_list_item_1);
		hhAdapter.setItemTextResource(android.R.id.text1);
		hhLv.setViewAdapter(hhAdapter);
		hhLv.setCyclic(true);
		hhLv.addClickingListener(new MyOnWheelClickedListener());
		
		NumericWheelAdapter mmAdapter = new NumericWheelAdapter(getActivity(), 0, 59, "%02d");
		mmAdapter.setItemResource(R.layout.simple_list_item_1);
		mmAdapter.setItemTextResource(android.R.id.text1);
		mmLv.setViewAdapter(mmAdapter);
        mmLv.setCyclic(true);
        mmLv.addClickingListener(new MyOnWheelClickedListener());
        
        Bundle ars = getArguments();
        NormalAlarm na = (NormalAlarm)ars.getSerializable(BundleKeys.NormalAlarmBundleKeys.NORMAL_ALARM_KEY);
        hhLv.setCurrentItem(na.getHh());
        mmLv.setCurrentItem(na.getMm());
	}

	
	
	@OnClick(R.id.ok_btn)
	void okClick(){
		myCallback.setHM(Integer.valueOf(hhLv.getCurrentItem()).byteValue(), Integer.valueOf(mmLv.getCurrentItem()).byteValue());
		getActivity().getSupportFragmentManager().beginTransaction().remove(this).commit();
	}
	@OnClick(R.id.cancel_btn)
	void cancelClick(){
		getActivity().getSupportFragmentManager().beginTransaction().remove(this).commit();
	}
	
	public class MyOnWheelClickedListener implements OnWheelClickedListener{
		@Override
		public void onItemClicked(AbstractWheel wheel, int itemIndex) {
			wheel.setCurrentItem(itemIndex,true);
			
		}
	}
	
}
